﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using IPC.Communication.Core;

namespace IPC.Communication.Core.Net
{
    public class AppSession
    {
        private byte[] _bytesHead = new byte[32];
        private DateTime _heartTime = DateTime.Now;
	    internal EndPoint UdpEndPoint = null;

	    internal Socket WorkSocket
	    {
		    get;
		    set;
	    }

	    internal SimpleHybirdLock HybirdLockSend
	    {
		    get;
		    set;
	    }

	    public string IpAddress
	    {
		    get;
		    internal set;
	    }

	    public IPEndPoint IpEndPoint
	    {
		    get;
		    internal set;
	    }

	    public string LoginAlias
	    {
		    get;
		    set;
	    }

	    public DateTime HeartTime
	    {
            get { return _heartTime; }
            set { _heartTime = value; }
	    }


	    public string ClientType
	    {
		    get;
		    set;
	    }

	    public string ClientUniqueID
	    {
		    get;
		    private set;
	    }

	    internal byte[] BytesHead
	    {
		    get { return _bytesHead; }
		    set { _bytesHead = value; }
	    }


	    internal int AlreadyReceivedHead
	    {
		    get;
		    set;
	    }

	    internal byte[] BytesContent
	    {
		    get;
		    set;
	    }

	    internal int AlreadyReceivedContent
	    {
		    get;
		    set;
	    }

	    internal string KeyGroup
	    {
		    get;
		    set;
	    }

	    public AppSession()
	    {
		    ClientUniqueID = Guid.NewGuid().ToString("N");
		    HybirdLockSend = new SimpleHybirdLock();
	    }

	    internal void Clear()
	    {
		    BytesHead = new byte[32];
		    AlreadyReceivedHead = 0;
		    BytesContent = null;
		    AlreadyReceivedContent = 0;
	    }

	    public override string ToString()
	    {
		    if (string.IsNullOrEmpty(LoginAlias))
		    {
			    return String.Format("AppSession[{0}]", IpEndPoint);
		    }
		    return String.Format("AppSession[{0}] [{1}]", IpEndPoint, LoginAlias);
	    }
    }
}
